﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using GtsServer.Common;

namespace GtsServer.DAL
{
    public class JournalReportDAL
    {
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(GtsServer.Model.JournalReportMould model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Journal(");
            strSql.Append("Imsi,Mac,UpStream,DownStream,RequestTime,Pid,Market,Name,Vesion,Position,Parameter,GameNum,GoldNum,EggNum)");
            strSql.Append(" values (");
            strSql.Append("@Imsi,@Mac,@UpStream,@DownStream,@RequestTime,@Pid,@Market,@Name,@Vesion,@Position,@Parameter,@GameNum,@GoldNum,@EggNum)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Imsi", SqlDbType.VarChar,50),
					new SqlParameter("@Mac", SqlDbType.VarChar,50),
					new SqlParameter("@UpStream", SqlDbType.Text),
					new SqlParameter("@DownStream", SqlDbType.Text),
					new SqlParameter("@RequestTime", SqlDbType.DateTime),
                    new SqlParameter("@Pid", SqlDbType.Int,4),
                    new SqlParameter("@Market", SqlDbType.VarChar,50),
                    new SqlParameter("@Name", SqlDbType.VarChar,50),
                    new SqlParameter("@Vesion", SqlDbType.VarChar,50),
                    new SqlParameter("@Position", SqlDbType.VarChar,50),
                    new SqlParameter("@Parameter", SqlDbType.Int,4),
                    new SqlParameter("@GameNum", SqlDbType.Int,4),
                    new SqlParameter("@GoldNum", SqlDbType.Int,4),
                    new SqlParameter("@EggNum", SqlDbType.Int,4)};
            parameters[0].Value = model.IMSI;
            parameters[1].Value = model.Mac;
            parameters[2].Value = model.UpStream;
            parameters[3].Value = model.DownStream;
            parameters[4].Value = model.Pid;
            parameters[5].Value = model.Market;
            parameters[6].Value = model.Name;
            parameters[7].Value = model.Vesion;
            parameters[8].Value = model.Position;
            parameters[9].Value = model.Parameter;
            parameters[10].Value = model.GameNum;
            parameters[11].Value = model.GoldNum;
            parameters[12].Value = model.EggNum;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters, ConnectionEnum.ConnectionReport);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public void AddReport(GtsServer.Model.JournalReportMould model)
        {
            IDataParameter[] parms = new IDataParameter[]{ 
                new SqlParameter("@IMSI",model.IMSI),
                new SqlParameter("@IMEI",model.IMEI),
                new SqlParameter("@Mac",model.Mac),
                new SqlParameter("@Pid",model.Pid),
                new SqlParameter("@UserAccount",model.UserAccount),
                new SqlParameter("@UpStream",model.UpStream),
                new SqlParameter("@DownStream",model.DownStream),
                new SqlParameter("@Vesion",model.Vesion),
                new SqlParameter("@RequestTime",model.RequestTime),
                new SqlParameter("@Position",model.Position),
                new SqlParameter("@Total_bonus",model.Total_bonus),
                new SqlParameter("@Match_coin",model.Match_coin),
                new SqlParameter("@GameNum",model.GameNum),
                new SqlParameter("@GoldNum",model.GoldNum)
            };
            DataSet ds = DbHelperSQL.RunProcedure("AddJournal", parms, "AddJournal", ConnectionEnum.ConnectionReport);
        }
        public DataSet GetListBySelect()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Name");
            strSql.Append(" FROM PositionInfo ");
            return DbHelperSQL.Query(strSql.ToString(), ConnectionEnum.ConnectionReport);
        }

        public DataSet GetList(int pageIndex, int pageSize, string potion, string strWhere = "")
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from (select *, ROW_NUMBER() OVER(Order by a.RequestTime DESC ) AS RowNumber from " + potion + " as a where");
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(strWhere);
            }
            strSql.Append(" ) as b where RowNumber BETWEEN " + ((pageIndex - 1) * pageSize + 1) + " and " + pageIndex * pageSize + " ");
            strSql.Append(";select count(1) from " + potion);
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql.Append(" where " + strWhere);
            }

            return DbHelperSQL.Query(strSql.ToString(), ConnectionEnum.ConnectionReport);
        }
    }
}
